#!/bin/bash 
# Here we figure out DL_SERVER, SOURCE_ROOT, WEB_DIR, etc.
. /vagrant/vagrant/functions.sh


if !(grep -q $HOST_ENV /etc/resolv.conf); then
  if !(grep -q search  /etc/resolv.conf); then
    echo "search $HOST_ENV" >> /etc/resolv.conf
  else
    sed -i "s/search.*$/& $HOST_ENV/g" /etc/resolv.conf
  fi
fi

if !(lsmod | grep -q vboxsf) ; then
  if !(grep -q mongo.$HOST_ENV /etc/hosts); then
  echo "127.0.0.1 mongo mongo.$HOST_ENV" >> /etc/hosts
  fi
  if !(grep -q mongodb.$HOST_ENV /etc/hosts); then
  echo "127.0.0.1 mongodb mongodb.$HOST_ENV" >> /etc/hosts
  fi
  if !(grep -q platform.$HOST_ENV /etc/hosts); then
  echo "127.0.0.1 platform.$HOST_ENV" >> /etc/hosts
  fi
  if !(grep -q cmis.$HOST_ENV /etc/hosts); then
  echo "127.0.0.1 cmis cmis.$HOST_ENV" >> /etc/hosts
  fi
  if !(grep -q restheart.$HOST_ENV /etc/hosts); then
  echo "127.0.0.1 restheart.$HOST_ENV" >> /etc/hosts
  fi
  if !(grep -q cas.$HOST_ENV /etc/hosts); then
  echo "127.0.0.1 cas cas.$HOST_ENV" >> /etc/hosts
  fi
fi

if !(grep -q s3.$HOST_ENV /etc/hosts); then
  echo "127.0.0.1 it-s4-fs-7aad91fd-e571-b6e5-571f-cebe20486ced.s3.$HOST_ENV" >> /etc/hosts
fi

if !(grep -q "mongodb.vagrant" /etc/hosts); then
  echo "127.0.0.1 mongodb.vagrant.foxitcloud.com" >> /etc/hosts
fi

if !(grep -q "mysql" /etc/hosts); then
  echo "127.0.0.1 mysql" >> /etc/hosts
fi

#the inner test env #20515 替换相应的环境参数为对应测试环境的。
INNER_TEST_ENV=( fz fztest itcws fz88 fz250 cndemo )
echo "--> The current test environment is ${PC_ENV} , set the params.php , cpdfApi.json's env config."
for i in "${INNER_TEST_ENV[@]}"
do
 if [ "$i" = "$PC_ENV" ]; then
    sed -i "s/vagrant/${PC_ENV}/g" /vagrant/webapp/common/config/params.php
	sed -i "s/vagrant/${PC_ENV}/g" /vagrant/webapp/connectedpdfapp/web/dll-config/1.0/cpdfApi.json
 fi 
done

if [[ "$YUM_SYS" =~ ^6\..* ]]; then
echo "--> Shut down iptables"
sudo service iptables stop
sudo chkconfig iptables off
fi

if [[ "$YUM_SYS" =~ ^7\..* ]]; then
echo "--> Shut down iptables"
sudo systemctl stop firewalld
sudo systemctl disable firewalld
fi

echo "--> Init Mongodb"
sudo sh /vagrant/vagrant/mongoinit.sh
sudo sh /vagrant/vagrant/mongoinit-cpdf.sh
sudo sh /vagrant/vagrant/mongodata-cpdf.sh
sudo sh /vagrant/vagrant/apimgt-data-init.sh
sudo sh /vagrant/vagrant/plugininit.sh

if [ -d /vagrant/webapp ]; then
echo_and_log "--> Migrating mongodb-migrate ..."
sudo $WEB_DIR/yii mongodb-migrate --interactive=0 > /dev/null
fi

echo "--> Init Fakes3"
sudo curl -s -X PUT http://it-s4-fs-7aad91fd-e571-b6e5-571f-cebe20486ced.s3.$HOST_ENV:1234


echo "--> Make sure Nginx has permission to web root"
sudo chown -R vagrant:vagrant /var/lib/foxit_ucx/


if [[ "$YUM_SYS" =~ ^7\..* ]]; then
sudo systemctl restart tomcat
fi

if [[ "$YUM_SYS" =~ ^6\..* ]]; then
sudo service tomcat restart
fi

echo "-- Restart fxplatform"
sudo service fxplatform restart

echo "--> Restart fxworker"
sudo service fxworker restart

echo "--> Restart fxrestheart"
sudo service fxrestheart restart

echo "--> Restart php-fpm"
if [[ "$YUM_SYS" =~ ^7\..* ]]; then
sudo systemctl restart php-fpm
fi

if [[ "$YUM_SYS" =~ ^6\..* ]]; then
sudo service php-fpm restart
fi

echo "--> Post_provision finished in ${SECONDS}s"

#make clean_assets
echo "--> The provision total cost $(expr `date +%s` -  `cat /tmp/provision-start.txt`) s this time . Contact us if you have any question ~"
